<?php

//@require_once(PATH_CLASS.'js.class.php');

class theme {
  
  const  TYPE_IND     = 'IND';
  const  TYPE_THEME   = 'THEME';
  const  TYPE_WIDGET  = 'WIDGET';
 
  public static $css     = array();
  public static $widgets = array();
  public static $default = array(
    'path' => array(
       'root'       => './css/'
      ,'theme'      => array(
           'css'  =>  './css/themes/?/'
          ,'js'   =>  './js/themes/?/'
        )
      )
  );
  
  public static     $_themes  = array();
  protected static $_theme   = '';

  protected $href = '';
  protected $more = '';
  protected $rank = 0;
  
  
  
  public function __toString(){
  }

  public static function add($themeName){
    
    if(!is_array($themeName)){
      $themeName = array($themeName);
    }
    foreach ($themeName as $theme){
      if(!in_array($theme, self::$_themes)){
        self::$_themes[$theme]=$theme;
      }
    }
  }
  
  public static function getThemeName(){
    return self::$_theme;
  }

  public static function sorter($a,$b){
    $rankA  = $a->rank;
    $rankB  = $b->rank;
    $diff   = ($rankA - $rankB);
    return ($diff);
  }  
  
  public static function getThemePath($what){
    $theme = self::getThemeName();
    if(isset(self::$default['path']) && isset(self::$default['path']['theme'])){
      if(isset(self::$default['path']['theme'][$what])){
        $theme = self::$default['path']['theme'][$what];
      }
    }
    return $theme;
  }
  
  public static function playTheme($theme){
    if($theme!=self::$_theme){
      $themes = self::$_themes;
      if(is_array($themes) && in_array($theme,$themes)){
        self::$_theme = $theme;
        css::addTheme(array('ui/jquery-ui.css','theme.css'));
        js::addTheme('theme.js',self::getThemePath('js'));
      }
    }
  }
  
  public static function playRandomTheme(){
    $themes = self::$_themes;
    $theme  = '';
    if(is_array($themes) && count($themes)>0){
      $theme = $themes[array_rand($themes)];
      self::playTheme($theme);
    }
    return $theme;
  }
  
  protected static function _render($what){
    $what = strtolower($what);
    
    switch($what){
      case 'js':
        break;
      case 'css':
        break;
    }
  }

  public static function render($what=null){
    $theme = self::getThemeName();
    if(empty($theme)){
      $theme = self::playRandomTheme();
    }
    
    if(!empty($theme)){
      switch($what){
        case null:
          self::_render('css');  
          self::_render('js');  
          break;
        case 'js':
        case 'JS':
          self::_render('js');  
          break;
        case 'css':
        case 'CSS':
          self::_render('css');  
          break;
      }
    }
    return $theme;
  }
  
  
}

